<!-- 团队项目进度 -->
<template>
  <div class="total-project-progress" v-if="loading">
    <el-tabs v-model="activeTabsName" class="demo-tabs" type="card" @tab-click="handleTabsClick">
      <el-tab-pane v-for="itemTab in projectCategoryItems" :label="itemTab.label" :name="itemTab.dictValue">
        <el-popover v-for="item in itemTab.projectsList" :key="item.id" :title="item.cname" width="300" placement="top" trigger="click">
          <template #reference>
            <div class="cell">
              <el-text class="label" truncated>
                {{ item.cname }}
              </el-text>
              <div class="progress">
                <el-progress :text-inside="true" :stroke-width="20" :percentage="item.progress" />
              </div>
            </div>
          </template>
          <div class="">
            <el-divider />
            <el-form-item label-width="90px" label="前期进度：">{{ item.preproductionProgress }}%</el-form-item>
            <el-form-item label-width="90px" label="中期进度：">{{ item.productionProgress }}%</el-form-item>
            <el-form-item label-width="90px" label="后期进度：">{{ item.postProductionProgress }}%</el-form-item>
            <el-form-item label-width="90px" label="负责人：">{{ item.producer }}</el-form-item>
          </div>
        </el-popover>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import Api from '../../api'
import { useProjectCategoryStore } from '../../stores'

const loading = ref(false)
const activeTabsName = ref(-1)
const projectCategoryItems = ref<any[]>([])
/**
 * 获取项目列表
 *
 * @returns 无返回值
 */
const setList = async (projectCategory = -1) => {
  const params: any = { projectStatus: 1, pageSize: 99999, display: 0 }
  if (projectCategory > -1) {
    params.projectCategory = projectCategory
  }

  const res = await Api.Projects.getProjectListByParams(params)
  let result = []
  if (res.code === 200) {
    result = res.rows.map((item: any) => {
      // console.log('item', item)
      const progress = Number(((item.postProductionProgress * item.postProductionProportion) / 100 + (item.productionProgress * item.productionProportion) / 100 + (item.preproductionProgress * item.preproductionProportion) / 100).toFixed(2))
      return {
        ...item,
        // progress: Number(((item.preproductionProgress * 1 + item.productionProgress * 1 + item.postProductionProgress * 1) / 3).toFixed(2)),
        progress,
      }
    })
  }
  return result
}

const setListCells = async () => {
  for (const item of projectCategoryItems.value) {
    const res = await setList(item.dictValue)
    item.projectsList = res
    item.total = res.length
    item.label = item.dictLabel + '(' + res.length + ')'
  }
  loading.value = true
}

const handleTabsClick = () => {
  console.log('activeTabsName', activeTabsName.value)
  // setList()
}

onMounted(async () => {
  loading.value = false
  // projectCategoryItems.value = []
  projectCategoryItems.value = [
    {
      dictLabel: '全部',
      dictValue: -1,
      total: 0,
    },
    ...useProjectCategoryStore().projectCategory,
  ]

  // setList()
  await setListCells()
})
</script>
<style scoped lang="scss">
.total-project-progress {
  padding: 0 0;
  .cell {
    display: flex;
    align-items: center;
    cursor: default;
    & + .cell {
      margin-top: 10px;
    }
  }
  .label {
    width: 200px;
    text-align: right;
    margin-right: 10px;
  }
  .progress {
    flex: 1;
  }
  .label2 {
    min-width: 200px;
  }
}
</style>
